package com.lantosec.hmrp.modules.eval.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.kit.tree.TreeNodeVo;
import com.lantosec.hmrp.core.log.ControllerLog;
import com.lantosec.hmrp.core.log.LogType;
import com.lantosec.hmrp.core.util.UUIDUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.modules.eval.entity.EvalType;
import com.lantosec.hmrp.modules.eval.service.IEvalTypeService;
import com.lantosec.hmrp.modules.eval.vo.EvalTypeVO;

import java.util.Arrays;
import java.util.List;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.RestController;
import com.lantosec.hmrp.core.mvc.BaseController;

/**
 *
 * 考评项目分类定义 控制器
 *
 * @author AutoCode
 * @since 2020-03-13
 */
@Api(value = "考评项目分类定义控制器", tags = "考评项目分类定义控制器")
@RestController
@RequestMapping("/eval/type")
@Log4j2
public class EvalTypeController extends BaseController {
    @Autowired
    private IEvalTypeService service;

    @GetMapping("getEvalParent")
    @ApiOperation(value = "查询父类为null的项目分类")
    public Result<List<EvalTypeVO>> getEvalTypeParent(){
        return Result.ok(entity2VO(service.list(new QueryWrapper<EvalType>())));
    }

    @GetMapping("getAll")
    @ApiOperation(value = "查询全部")
    public Result<List<EvalTypeVO>> all(@ApiParam(value = "分类名称")@RequestParam(name = "evalTypeName",required = false) String evalTypeName){
        List<EvalTypeVO> evalTypeVOList = service.getAll(evalTypeName);
        return Result.ok(evalTypeVOList);
    }

    @GetMapping("getTree")
    @ApiOperation(value = "查询考评项目树")
    public Result<List<TreeNodeVo>> getTree(@ApiParam(value = "考评项目分类id") @RequestParam(name="id",required = false) String id){
        List<TreeNodeVo> treeNodeVos = service.getTree(id);
        return Result.ok(treeNodeVos);
    }

    @GetMapping("query")
    @ApiOperation(value = "分页查询")
    public Result<Page<EvalTypeVO>> page(@ApiParam(value = "分页对象", required = true) PageVO<EvalType> page,
                                         @ApiParam(value = "分类名称")@RequestParam(name = "evalTypeName",required = false) String evalTypeName){
        page = service.page(page,evalTypeName);
        return Result.ok(entity2VO(page));
    }

    @GetMapping("get/{id}")
    @ApiOperation(value = "主键查询")
    public Result<EvalTypeVO> get(@ApiParam(value = "ID", required = true) @PathVariable(name="id") String id){
        return Result.ok(entity2VO(service.getById(id)));
    }

    @PostMapping("create")
    @ControllerLog(name = "新增考评项目分类", type = LogType.add)
    @ApiOperation(value = "新增数据")
    public Result<String> create(@ApiParam(value = "考评项目分类定义VO对象", required = true) @RequestBody @Validated EvalTypeVO vo){
        EvalType entity = vo.convertToEntity();
        String evalTypeId = service.saveEvalType(entity);
        return Result.ok(evalTypeId);
    }

    @PutMapping("update")
    @ControllerLog(name = "修改考评项目分类", type = LogType.update)
    @ApiOperation(value = "修改数据")
    public Result<Void> update(@ApiParam(value = "考评项目分类定义VO对象", required = true) @RequestBody EvalTypeVO vo){
        EvalType entity = vo.convertToEntity();
        service.updateEvalType(entity);
        return Result.ok();
    }

    @DeleteMapping(value = "delete/{ids}")
    @ControllerLog(name = "逻辑删除考评项目分类", type = LogType.delete)
    @ApiOperation("逻辑删除")
    public Result<Void> delete(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name="ids") String ids){
        if(StringUtils.isNotEmpty(ids)){
            service.removeByIds(Arrays.asList(ids.split(",")));
        }
        return Result.ok();
    }

    @PutMapping("updateOrder")
    @ControllerLog(name = "修改考评项目分类排序", type = LogType.update)
    @ApiOperation(value = "修改排序")
    public Result<Void> updateOrder(@ApiParam(value = "考评项目分类ID", required = true) @RequestParam(value = "id") String id,
                                    @ApiParam(value = "考评项目分类排序值", required = true) @RequestParam(value = "evalTypeOrder",required = false) int evalTypeOrder){
        service.updateOrder(id,evalTypeOrder);
        return Result.ok();
    }

    @GetMapping("getByEvalTarget")
    @ApiOperation(value = "根据被考评对象查询项目")
    public Result<List<EvalTypeVO>> getByEvalTarget(@ApiParam(value = "ID") @RequestParam(name = "evalTarget",required = false) Integer evalTarget) {
        List<EvalTypeVO> evalTypeVOList = service.getByEvalTarget(evalTarget);
        return Result.ok(evalTypeVOList);
    }

    @GetMapping("getByEvalTargetApply")
    @ApiOperation(value = "根据被考评对象查询申报项目")
    public Result<List<EvalTypeVO>> getByEvalTargetApply(@ApiParam(value = "ID") @RequestParam(name = "evalTarget",required = false) Integer evalTarget) {
        List<EvalTypeVO> evalTypeVOList = service.getByEvalTargetApply(evalTarget);
        return Result.ok(evalTypeVOList);
    }

}
